home *** CD-ROM | disk | FTP | other *** search
/ Revista do CD-ROM 97 / CD-ROM 97 / CD-ROM 97.iso / internet / ghostzilla / ghsetup.exe / chrome / comm.jar / content / editor / EdFormProps.js < prev    next >
Encoding:
JavaScript  |  2002-04-09  |  4.4 KB  |  150 lines

  1. /* ***** BEGIN LICENSE BLOCK *****
  2.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3.  *
  4.  * The contents of this file are subject to the Mozilla Public License Version
  5.  * 1.1 (the "License"); you may not use this file except in compliance with
  6.  * the License. You may obtain a copy of the License at
  7.  * http://www.mozilla.org/MPL/
  8.  *
  9.  * Software distributed under the License is distributed on an "AS IS" basis,
  10.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11.  * for the specific language governing rights and limitations under the
  12.  * License.
  13.  *
  14.  * The Original Code is Form Properties Dialog.
  15.  *
  16.  * The Initial Developer of the Original Code is
  17.  * Neil Rashbrook.
  18.  * Portions created by the Initial Developer are Copyright (C) 2001
  19.  * the Initial Developer. All Rights Reserved.
  20.  *
  21.  * Contributor(s): Neil Rashbrook <neil@parkwaycc.co.uk>
  22.  *
  23.  * Alternatively, the contents of this file may be used under the terms of
  24.  * either the GNU General Public License Version 2 or later (the "GPL"), or
  25.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  26.  * in which case the provisions of the GPL or the LGPL are applicable instead
  27.  * of those above. If you wish to allow use of your version of this file only
  28.  * under the terms of either the GPL or the LGPL, and not to allow others to
  29.  * use your version of this file under the terms of the MPL, indicate your
  30.  * decision by deleting the provisions above and replace them with the notice
  31.  * and other provisions required by the GPL or the LGPL. If you do not delete
  32.  * the provisions above, a recipient may use your version of this file under
  33.  * the terms of any one of the MPL, the GPL or the LGPL.
  34.  *
  35.  * ***** END LICENSE BLOCK ***** */
  36.  
  37. var gForm;
  38. var insertNew;
  39. var formElement;
  40. var formActionWarning;
  41.  
  42. function Startup()
  43. {
  44.   if (!InitEditorShell())
  45.     return;
  46.  
  47.   gForm = {
  48.     Name:     document.getElementById("FormName"),
  49.     Action:   document.getElementById("FormAction"),
  50.     Method:   document.getElementById("FormMethod"),
  51.     EncType:  document.getElementById("FormEncType"),
  52.     Target:   document.getElementById("FormTarget")
  53.   }
  54.   gDialog.MoreSection = document.getElementById("MoreSection");
  55.   gDialog.MoreFewerButton = document.getElementById("MoreFewerButton");
  56.   gDialog.RemoveForm = document.getElementById("RemoveForm")
  57.  
  58.   // Get a single selected form element
  59.   var tagName = "form";
  60.   formElement = editorShell.GetSelectedElement(tagName);
  61.   if (!formElement)
  62.     formElement = editorShell.GetElementOrParentByTagName(tagName, editorShell.editorSelection.anchorNode);
  63.   if (!formElement)
  64.     formElement = editorShell.GetElementOrParentByTagName(tagName, editorShell.editorSelection.focusNode);
  65.  
  66.   if (formElement)
  67.   {
  68.     // We found an element and don't need to insert one
  69.     insertNew = false;
  70.     formActionWarning = formElement.hasAttribute("action");
  71.   }
  72.   else
  73.   {
  74.     insertNew = true;
  75.     formActionWarning = true;
  76.  
  77.     // We don't have an element selected,
  78.     //  so create one with default attributes
  79.  
  80.     formElement = editorShell.CreateElementWithDefaults(tagName);
  81.     if (!formElement)
  82.     {
  83.       dump("Failed to get selected element or create a new one!\n");
  84.       window.close();
  85.       return;
  86.     }
  87.     // Hide button removing existing form
  88.     gDialog.RemoveForm.setAttribute("hidden", "true");
  89.   }
  90.  
  91.   // Make a copy to use for AdvancedEdit
  92.   globalElement = formElement.cloneNode(false);
  93.  
  94.   InitDialog();
  95.  
  96.   InitMoreFewer();
  97.  
  98.   SetTextboxFocus(gForm.Name);
  99.  
  100.   SetWindowLocation();
  101. }
  102.  
  103. function InitDialog()
  104. {
  105.   for (var attribute in gForm)
  106.     gForm[attribute].value = globalElement.getAttribute(attribute);
  107. }
  108.  
  109. function RemoveForm()
  110. {
  111.   RemoveElementKeepingChildren(formElement);
  112.   SaveWindowLocation();
  113.   window.close();
  114. }
  115.  
  116. function ValidateData()
  117. {
  118.   for (var attribute in gForm)
  119.   {
  120.     if (gForm[attribute].value)
  121.       globalElement.setAttribute(attribute, gForm[attribute].value);
  122.     else
  123.       globalElement.removeAttribute(attribute);
  124.   }
  125.   return true;
  126. }
  127.  
  128. function onAccept()
  129. {
  130.   if (formActionWarning && !gForm.Action.value)
  131.   {
  132.     AlertWithTitle(GetString("Alert"), GetString("NoFormAction"));
  133.     gForm.Action.focus();
  134.     formActionWarning = false;
  135.     return false;
  136.   }
  137.   // All values are valid - copy to actual element in doc or
  138.   //   element created to insert
  139.   ValidateData();
  140.  
  141.   editorShell.CloneAttributes(formElement, globalElement);
  142.  
  143.   if (insertNew)
  144.     InsertElementAroundSelection(formElement);
  145.  
  146.   SaveWindowLocation();
  147.  
  148.   return true;
  149. }
  150.